/****************************************************************************
 * 2025.9 阿桑的笔记本
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using IE.RSB;
namespace QFramework.Example
{
	public partial class SurroundingsPlanes : UIElement,IController
	{


        public BasicTrainingDataManager dataManager;
        public EnvironmentProperties getEnvironmentProperties;
        public BulletProperties bulletProperties;  

        private void Awake()
		{
            dataManager = DataManagerProvider.Current;
            Debug.Log(dataManager.gameObject.name);
            getEnvironmentProperties = dataManager.GetEnvironmentProperties();
            bulletProperties = dataManager.GetBulletProperties();
            BtnClicks();
            ReadEvents();
        }
        

		protected override void OnBeforeDestroy()
		{

		}

        public override void ReadEvents()
        {
            base.ReadEvents();
            this.RegisterEvent<SurroundCommandEvent>(e =>
            {


                Debug.Log("数据相应");
                ///空气阻力
                AirFriction.onValueChanged.AddListener(value =>
                {
                    value = bulletProperties.m_affectedByAirResistance;

                    //value = bulletProperties.m_
                    //value = FreePlayDataManager.Instance.bulletProperties.m_affectedByAirResistance;
                });

                GravityFiled.text = getEnvironmentProperties.m_gravity.ToString();

                TemperatureFiled.text = getEnvironmentProperties.m_temperature.ToString();

                AirPressureFiled.text = getEnvironmentProperties.m_airPressure.ToString();

                WindPlanes.XWind.text = getEnvironmentProperties.m_windSpeed.x.ToString();

                WindPlanes.YWind.text = getEnvironmentProperties.m_windSpeed.y.ToString();

                WindPlanes.ZWind.text = getEnvironmentProperties.m_windSpeed.z.ToString();

                AirFriction.isOn = bulletProperties.m_affectedByAirResistance;

                //GravityFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_gravity.ToString();

                //TemperatureFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_temperature.ToString();

                //AirPressureFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_airPressure.ToString();

                //WindPlanes.XWind.text = FreePlayDataManager.Instance.EnvironmentProperties.m_windSpeed.x.ToString();

                //WindPlanes.YWind.text = FreePlayDataManager.Instance.EnvironmentProperties.m_windSpeed.y.ToString();

                //WindPlanes.ZWind.text = FreePlayDataManager.Instance.EnvironmentProperties.m_windSpeed.z.ToString();

                //AirFriction.isOn = FreePlayDataManager.Instance.bulletProperties.m_affectedByAirResistance;


            });


            AirFriction.onValueChanged.AddListener(value =>
            {
                value = bulletProperties.m_affectedByAirResistance;

                //value = bulletProperties.m_
                //value = FreePlayDataManager.Instance.bulletProperties.m_affectedByAirResistance;
            });

            GravityFiled.text = getEnvironmentProperties.m_gravity.ToString();

            TemperatureFiled.text = getEnvironmentProperties.m_temperature.ToString();

            AirPressureFiled.text = getEnvironmentProperties.m_airPressure.ToString();

            WindPlanes.XWind.text = getEnvironmentProperties.m_windSpeed.x.ToString();

            WindPlanes.YWind.text = getEnvironmentProperties.m_windSpeed.y.ToString();

            WindPlanes.ZWind.text = getEnvironmentProperties.m_windSpeed.z.ToString();

            AirFriction.isOn = bulletProperties.m_affectedByAirResistance;

        }


        

        public override void BtnClicks()
        {
            //throw new NotImplementedException();
            WindPos.onClick.AddListener(() =>
            {
                WindPlanes.Show();
            });

            ///开启或关闭风阻
            AirFriction.onValueChanged.AddListener(value =>
            {

                dataManager.gameScenceDatas.AirRestance = value;

                //FreePlayDataManager.Instance.bulletProperties.m_affectedByAirResistance = value;
                //FreePlayDataManager.Instance.gameModel.gameScence.AirRestance = value;

            });

            ///归零点
            GunZeroPoint.onClick.AddListener(() =>
            {
               fireZeroPanel.Show();
            });


            //保存按钮
            SaveBtn.onClick.AddListener(() => 
            {

                Debug.Log(Patterns.GameTag);

                switch (Patterns.GameTag)
                {
                    case "FreeGame":
                        // SaveSystemData();
                        DataSavePanel.Show();
                        break;
                    case "BasicGame":
                        //SetGameValue();

                        SetData();
                       
                        UIKit.CloseAllPanel();
                        break;
                }

                //显示保存提示UI
                
            });

           


        }

        public  IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }

        


        /// <summary>
        ///先设置数据到数据组，再设置到配置文件
        /// </summary>
        void SetData()
        {
            dataManager.gameScenceDatas.AirPressure = float.Parse( AirPressureFiled.text);
            dataManager.gameScenceDatas.Gravity = float.Parse( GravityFiled.text);
            dataManager.gameScenceDatas.WindSpeedZ = float.Parse(WindPlanes.ZWind.text);
            dataManager.gameScenceDatas.WindSpeedX = float.Parse(WindPlanes.XWind.text);
            dataManager.gameScenceDatas.WindSpeedY = float.Parse(WindPlanes.YWind.text);
            for (int i = 0;i<fireZeroPanel.inputFields.Count;i++)
            {
                dataManager.gameScenceDatas.ZeroDistances[i] = float.Parse(fireZeroPanel.inputFields[i].text);
            }
            dataManager.gameScenceDatas.AirRestance = AirFriction.isOn;
            dataManager.gameScenceDatas.Gravity = float.Parse(GravityFiled.text);
            //dataManager.gameScenceDatas.
            dataManager.BisciSobjSetData.SetGameScence();
        }

        /// <summary>
        /// 核对输入框里是否有不合法内容
        /// </summary>
        public  void CheckInputFiledText()  
        {
            if (string.IsNullOrEmpty(GravityFiled.text))
            {
                GravityFiled.text = getEnvironmentProperties.m_gravity.ToString();/*dataManager.gete.m_*/
                   // GravityFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_gravity.ToString();


            }
            else if(float.Parse(GravityFiled.text) < 0f)
            {
                GravityFiled.text = getEnvironmentProperties.m_gravity.ToString();
                //GravityFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_gravity.ToString();
            }

            if (string.IsNullOrEmpty(TemperatureFiled.text))
            {
                TemperatureFiled.text = getEnvironmentProperties.m_temperature.ToString();
                   // TemperatureFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_gravity.ToString();


                // TemperatureFiled.text = DataManager.Instance.EnvironmentProperties.m_temperature.ToString();
            }
            else if (float.Parse(TemperatureFiled.text) < 0f)
            {
                TemperatureFiled.text = getEnvironmentProperties.m_temperature.ToString();
                // TemperatureFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_gravity.ToString();
            }

            if (string.IsNullOrEmpty(AirPressureFiled.text))
            {
                AirPressureFiled.text = getEnvironmentProperties.m_airPressure.ToString();

                    //AirPressureFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_gravity.ToString();

                //AirPressureFiled.text = DataManager.Instance.EnvironmentProperties.m_airPressure.ToString();
            }
            else if (float.Parse(AirPressureFiled.text) < 0f)
            {
                AirPressureFiled.text = getEnvironmentProperties.m_airPressure.ToString();
                //AirPressureFiled.text = FreePlayDataManager.Instance.EnvironmentProperties.m_gravity.ToString();
            }
        }
    }
}